﻿
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TalentDesk.Contracts.Ef;
using TalentDesk.Contracts.Enums;
using TalentDesk.Mvc.Controllers;
using TalentDesk.Mvc.HelperClasses;
using TalentDesk.Mvc.Models;


namespace TalentDesk.Mvc.Controllers
{
    public class ProjectController : BaseController
    {

        public ActionResult ProjectList()
        {

            return View();
        }

        [HttpPost]
        public ActionResult _NewProject()
        {
            Project oProjects = new Project();
            return PartialView("EditProject", oProjects);
        }

        [HttpPost]
        public ActionResult _EditProject(string projectId)
        {
            //return MessageBox("asdasd");
            Project oProjects = new Project();
            try
            {
                oProjects = ProjectProxy.GetProjectByProjectId(Convert.ToInt32(projectId), true);
            }
            catch (Exception ex)
            {
                return MessageBox(ex.Message, EnumMessageType.WarningMessage);
            }
            Session["Project"] = oProjects;
            return PartialView("EditProject", oProjects);
        }
           [HttpPost]
        public ActionResult ProjectView(string projectId)
        {
            
            Project oProjects = new Project();
            try
            {
                oProjects = ProjectProxy.GetProjectByProjectId(Convert.ToInt32(projectId), true);
            }
            catch (Exception ex)
            {
                return MessageBox(ex.Message, EnumMessageType.WarningMessage);
            }

            return PartialView( oProjects);
        }

        

        public ActionResult _GetProject([DataSourceRequest] DataSourceRequest request)
        {
            List<Project> lProject = ProjectProxy.GetProjects(false);
            return Json(ProjectProxy.GetProjects(false).ToDataSourceResult(request));
        }

      
        public ActionResult ProjectKaydet( IEnumerable<HttpPostedFileBase> files,Project model)
        {

            try
            {
                Project project = null;
                if (Session["Project"] !=null)
                {
                    project = (Project)Session["Project"];
                }


                if (project == null)
                {
                    project = new Project();
                    project.CreateTime = DateTime.Now;
                    project.CreateUserId = AuthenticationHelpers.GetLoggedInUser().UserId;
                    project.IsActive = true;
                }
                else
                {
                    project.UpdateTime = DateTime.Now;
                    project.UpdateUserId = AuthenticationHelpers.GetLoggedInUser().UserId;
                }

               

                List<Attachment> lAttachmentsNew = new List<Attachment>();
                if (files != null)
                {

                    foreach (var file in files)
                    {
                        if (file != null)
                        {

                            var fileName = Path.GetFileName(file.FileName);
                            var physicalPath = Path.Combine(Server.MapPath("~/App_Data/Files"), fileName);
                            Attachment oAttachment = new Attachment();
                            oAttachment.AttachmentName = fileName;

                            oAttachment.CreateTime = DateTime.Now;
                            oAttachment.CreateUserId = AuthenticationHelpers.GetLoggedInUser().UserId;
                            oAttachment.FileName = fileName;
                            oAttachment.FileUrl = physicalPath;
                            oAttachment.IsActive = true;
                            oAttachment.ProjectId = model.ProjectId;

                            file.SaveAs(physicalPath);
                            lAttachmentsNew.Add(oAttachment);
                        }
                    }
                }

                project.DueDate = model.DueDate;
                project.Progress = model.Progress;
                project.ProjectDescription = model.ProjectDescription;
                project.ProjectName = model.ProjectName;
                project.ProjectTypeId = model.ProjectTypeId;
                project.StartDate = model.StartDate;
                
              
                ProjectProxy.SaveProject(project, model.ProjectTeams.ToList(), lAttachmentsNew);

                return MessageBox("Kayıt işlemi başarılı.", p_sExtraScript: " $('#modal').animate({ 'right': -$('#modal').width() }, 300, 'swing', function () { $('#modal').hide(); });");
            }
            catch (Exception ex)
            {
                return MessageBox(ex.Message, EnumMessageType.ErrorMessage);
            }



        }

        //public JsonResult GetProjectTypes()
        //{
        //    return Json(CacheHelper.GetProjectTypes(), JsonRequestBehavior.AllowGet);
        //}
        //public ActionResult ProjectTypeList()
        //{
        //    IProject objProject = Wcf.Proxy<IProject>();
        //    List<ProjectTypes> lProjectType = new List<ProjectTypes>();
        //    lProjectType = objProject.GetProjectTypes();
        //    return View(lProjectType);
        //}

        //public ActionResult AddProjectType()
        //{
        //    return View();
        //}

        //[AcceptVerbs(HttpVerbs.Post)]
        //public ActionResult AddProjectType(ProjectTypeModel model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        try
        //        {
        //            var mapper = ObjectMapperManager.DefaultInstance.GetMapper<ProjectTypeModel, ProjectTypes>();
        //            ProjectTypes oProjectTypes = new ProjectTypes();
        //            mapper.Map(model, oProjectTypes);

        //            oProjectTypes.CreateTime = DateTime.Now;
        //            oProjectTypes.CreateUserId = ActiveUser.UserId;

        //            IProject objProject = Wcf.Proxy<IProject>();
        //            objProject.AddProjectType(oProjectTypes);
        //            MessageBox(String.Format("{0} project type was saved.", oProjectTypes.ProjectType));
        //            return RedirectToAction("ProjectTypeList", "Project");
        //        }
        //        catch (Exception ex)
        //        {
        //            MessageBox(ex.Message.ToString(), EnumMessageType.WarningMessage);
        //        }

        //    }

        //    return View(model);
        //}

        //public ActionResult ProjectList()
        //{
        //    return View(GetProjects());
        //}



        //private static IEnumerable<Projects> GetProjects()
        //{
        //    IProject objProject = Wcf.Proxy<IProject>();
        //    return objProject.GetProjects().AsEnumerable();
        //}

        //public ActionResult AddProject()
        //{
        //    return View();
        //}



        //[AcceptVerbs(HttpVerbs.Post)]
        //public ActionResult AddProject(ProjectModel model)
        //{
        //    return View();
        //}

        //public ActionResult EditProject(string projectid)
        //{
        //    ProjectModel oProjectModel = new ProjectModel();

        //    try
        //    {
        //        Projects oProjects = new Projects();
        //        IProject objProject = Wcf.Proxy<IProject>();
        //        oProjects = objProject.GetProjectByProjectId(Convert.ToInt32(projectid), true);
        //        var mapper = EmitMapper.ObjectMapperManager.DefaultInstance.GetMapper<Projects, ProjectModel>();
        //        mapper.Map(oProjects, oProjectModel);

        //        oProjectModel.ProjectTeam = (from t in oProjects.ProjectTeams
        //                                     select t.UserId.ToString()).ToList<string>();

        //        IAttachment objAttachment = Wcf.Proxy<IAttachment>();
        //        oProjectModel.Attachments = objAttachment.GetAttachmentsByPrentIdAndAttachmentType(Convert.ToInt32(projectid), EnumAttachmentType.Project);

        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox(ex.Message, EnumMessageType.WarningMessage);
        //    }

        //    return View(oProjectModel);
        //}



        //[AcceptVerbs(HttpVerbs.Post)]
        //public ActionResult EditProject(ProjectModel model, IEnumerable<HttpPostedFileBase> files)
        //{
        //    try
        //    {
        //        List<Attachments> lAttachmentsNew = new List<Attachments>();
        //        if (files != null)
        //        {

        //            foreach (var file in files)
        //            {
        //                var fileName = Path.GetFileName(file.FileName);
        //                var physicalPath = Path.Combine(Server.MapPath("~/App_Data/Files"), fileName);
        //                Attachments oAttachments = new Attachments();
        //                oAttachments.AttachmentName = fileName;
        //                oAttachments.AttachmentTypeId = (int)EnumAttachmentType.Project;
        //                oAttachments.CreateTime = DateTime.Now;
        //                oAttachments.CreateUserId = ActiveUser.UserId;
        //                oAttachments.FileName = fileName;
        //                oAttachments.FileUrl = physicalPath;
        //                oAttachments.IsActive = true;
        //                oAttachments.ParentId = model.ProjectId;

        //                file.SaveAs(physicalPath);
        //                lAttachmentsNew.Add(oAttachments);
        //            }
        //        }

        //        Projects oProject = new Projects();
        //        IProject objProject = Wcf.Proxy<IProject>();
        //        oProject = objProject.GetProjectByProjectId(model.ProjectId, false);
        //        var mapper = EmitMapper.ObjectMapperManager.DefaultInstance.GetMapper<ProjectModel, Projects>();
        //        mapper.Map(model, oProject);
        //        oProject.UpdateTime = DateTime.Now;
        //        oProject.UpdateUserId = ActiveUser.UserId;

        //        objProject.UpdateProject(oProject, model.ProjectTeam, lAttachmentsNew);
        //        MessageBox("Project was update.");

        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox(ex.Message, EnumMessageType.WarningMessage);
        //    }



        //    return View(model);
        //}



        //public ActionResult Save(IEnumerable<HttpPostedFileBase> files)
        //{
        //     The Name of the Upload component is "files"
        //    if (files != null)
        //    {
        //        foreach (var file in files)
        //        {
        //             Some browsers send file names with full path.
        //             We are only interested in the file name.
        //            var fileName = Path.GetFileName(file.FileName);
        //            var physicalPath = Path.Combine(Server.MapPath("~/App_Data/Files"), fileName);

        //             The files are not actually saved in this demo
        //             file.SaveAs(physicalPath);
        //        }
        //    }

        //     Return an empty string to signify success
        //    return Content("");
        //}

        //public ActionResult Remove(string[] fileNames)
        //{
        //     The parameter of the Remove action must be called "fileNames"

        //    if (fileNames != null)
        //    {
        //        foreach (var fullName in fileNames)
        //        {
        //            var fileName = Path.GetFileName(fullName);
        //            var physicalPath = Path.Combine(Server.MapPath("~/App_Data"), fileName);

        //             TODO: Verify user permissions

        //            if (System.IO.File.Exists(physicalPath))
        //            {
        //                 The files are not actually removed in this demo
        //                 System.IO.File.Delete(physicalPath);
        //            }
        //        }
        //    }

        //     Return an empty string to signify success
        //    return Content("");
        //}

        //public FilePathResult DownloadFile(string attachmentId)
        //{
        //    IAttachment objAttachment = Wcf.Proxy<IAttachment>();
        //    Attachments oAttachment = objAttachment.GetAttachmentsByAttachmentId(Convert.ToInt32(attachmentId));
        //    string path = oAttachment.FileUrl;

        //    return File(path, "text/plain", oAttachment.FileName);
        //}


    }
}
